#思路
首先记录各个节点的上下级关系,然后深度优先遍历更新各个节点的price,最后将所有节点的stock(如果不是零售商则为0)和对应的price相乘得到。
#代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using namespace std;
int n;
double rootPrice,percent;
double price[MAX];
double stock[MAX];
vector<vector<int> > chain;
int updatePrice(int curid, double curPrice)//深度优先更新价格
{
for(int i=0;i<chain[curid].size();i++)
{
int child = chain[curid][i];
if(child != -1)
{
price[child] = curPrice * (1+(double)percent/100);
updatePrice(child, price[child]);
}
}
return 0;
}
int main()
{
cin>>n>>rootPrice>>percent;
memset(price,0,sizeof(price));
memset(stock,0,sizeof(stock));
for(int i=0;i<n;i++)
{
int num,id;
cin>>num;
vector<int> tmp;
if(num)//for those non-retailers push back their retailers or distrubutor
while(num--)
{
cin>>id;
tmp.push_back(id);
}
else//for those retailers, push back -1 to indicate it's a retailer,and update stock;
{
double st;
cin>>st;
stock[i]=st;
tmp.push_back(-1);
}
chain.push_back(tmp);
}
double sum=0;
updatePrice(0,rootPrice);
for(int i=0;i<n;i++)
{
sum += price[i]*stock[i];
}
printf("%.1f\n",sum);
return 0;
}